package com.apps.essentials;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class GetUserLocation extends Service implements LocationListener{

	private final Context mcontext = null;
	
	boolean isGPSEnabled = false;//status gps
	boolean isNetworkEnabled = false;//status network
	boolean islocationAvailable = false;//status gps location
	Location location = null;//location
	double latitude;//latitude to be sent to main activity
	double longitude;//longitude to be sent to main activity
	private static final long MIN_DISTANCE_UPDATES_CHANGE = 10;//in meters
	private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;//in minute 1
	protected LocationManager locationManager;//location be updated via this class 
	
	private GeoPoint gp=null;
	
	public Location getLocation(){
		try{
			locationManager = (LocationManager) mcontext.getSystemService(LOCATION_SERVICE);//instantiate sa locationmanager class
			isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);//then pasa diri check sa gps status
			isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);//pariha lang gihapon diri check network enabled(wifi)
			
			if(!isGPSEnabled && !isNetworkEnabled){
				//request to enabled the gps and network
			}else{
				islocationAvailable = true;
				if (isNetworkEnabled) {
					locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_UPDATES_CHANGE, this);
					Log.d("Network", "Network Enabled");
					if (locationManager != null) {
						location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if (location != null) {
							latitude = location.getLatitude();
							longitude = location.getLongitude();
						}
					}
				}
				if (isGPSEnabled) {
					if (location == null) {
						locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_UPDATES_CHANGE, this);
						Log.d("GPS", "GPS Enabled");
						if (locationManager != null) {
							location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
							if (location != null) {
								latitude = location.getLatitude();
								longitude = location.getLongitude();
							}
						}
					}
				}				
			}
		}catch(Exception ie){
			ie.printStackTrace();
		}
		return location;
	}
	
	public void stopGPS(){
		if (locationManager != null) {
			locationManager.removeUpdates(GetUserLocation.this);
		}		
	}
	
	public double getUserLatitude(){
		if(location != null){
			latitude = location.getLatitude();
		}
		return latitude;
	}
	
	public double getUserLongitude(){
		if(location != null){
			longitude = location.getLongitude();
		}
		return longitude;
	}
	
	public boolean isLocationAvailable(){
		return islocationAvailable;
	}
	
	public void showSettingsAlert() {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(mcontext);

		
		alertDialog.setTitle("GPS is settings");//title 
		alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");	// message

		// yes
		alertDialog.setPositiveButton("Settings",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(
								Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						mcontext.startActivity(intent);
					}
				});

		// no
		alertDialog.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});

		
		alertDialog.show();// Alert Message
	}	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
